public class Test2{
	public static void main(String[] args){
		//测试
		int[] arr1 = {1, -2, 3, 5, -1};
		int[] arr2 = {1, -2, 3, -8, 5, 1};
		int[] arr3 = {1, -2, 3,-2, 5, 1};
		System.out.println("数组1的最大子数组和: " + maxSubarraySum(arr1)); // 应输出8
		System.out.println("数组2的最大子数组和: " + maxSubarraySum(arr2)); // 应输出6
        	System.out.println("数组3的最大子数组和: " + maxSubarraySum(arr3)); // 应输出7
	}
	public static int maxSubarraySum(int[] arr){
		if(arr == null || arr.length == 0){
			throw new IllegalArgumentException("数组不能为空");
		}
		int currentMax = arr[0];
		int globalMax = arr[0];
		for( int i = 1 ; i < arr.length ; i++ ){
			currentMax = Math.max(arr[i] , arr[i] + currentMax); 
			globalMax = Math.max(currentMax , globalMax);
		}
		return globalMax;
	}
}